#!/usr/bin/perl

# Early in your program:

use LWP 5.64; # Loads all important LWP classes, and makes
				#  sure your version is reasonably recent.
				
use DBI;

@ns_headers = 
(
	'User-Agent' => 'Mozilla/4.76 [en] (Win98; U)',
	'Accept-Charset' => 'iso-8859-1,*,utf-8',
	'Accept-Language' => 'en-US',
);
$browser = LWP::UserAgent->new;
####################################################################################################
####################################################################################################
####################################################################################################
sub trim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}
sub now()
{
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
	my $datestring = sprintf("%4d-%02d-%02d %02d:%02d:%02d",($year + 1900),($mon+1),$mday,$hour,$min,$sec);
	return $datestring
}
sub parseList
{
	$baseURL = $_[0];
	$id = $_[1];
	
	$countLetter->execute();
	$countLetter->bind_columns(\$initialCount);
	$countLetter->fetch();
	$url = "$baseURL/$id";
	my $response = $browser->get( $url, @ns_headers );
	unless ($response->is_success) 
	{
		warn "Unable to load page for '$id'\n";
		next;
	}
	$string = $response->decoded_content;
	while ($string =~ m/<a href="http:\/\/watch-movies-az.com\/movie\/([^"\n]+)[^>]+>\s*\n*\s*(.*)\s+\((\d*)\)\s*<\/a>/g) 
	{
		$code = $1;
		$name = trim($2);
		$year = $3;
		if( substr($name, 0, 1) =~ /[a-zA-Z]/ )
		{
			$char = uc(substr($name, 0, 1));
		}
		else
		{
			$char = '#';
		}
		$insert->execute($name,$year,$char,$code);
	}
	$countLetter->execute();
	$countLetter->bind_columns(\$finalCount);
	$countLetter->fetch();
	$count = $finalCount - $initialCount;
	if( $count > 0 )
	{
		print "$id: $count rows added\n";
	}
	return $count;
}
####################################################################################################
####################################################################################################
####################################################################################################
print "~~~ MOVIELIST.PL START TIME: ".now()." ~~~\n";
my $db = DBI -> connect("dbi:Pg:dbname=watchmovies;host=localhost;port=4232","watchMoviesUser","Movies42FTW") || die $DBI::errstr;
$insert = $db->prepare('SELECT F_TMovies_Add_Movie(?,?,?,?);');
$countLetter = $db->prepare('SELECT COUNT(*) FROM TMovies');
####################################################################################################
####################################################################################################
####################################################################################################
$totalcount = parseList("http://watch-movies-az.com", "latest");

foreach $id (("A" .. "Z"))
{
	$count = parseList("http://watch-movies-az.com/letter", $id);
	$totalcount = $totalcount + $count;
}
foreach $id ((1980 .. 2010))
{
	$count = parseList("http://watch-movies-az.com/year", $id);
	$totalcount = $totalcount + $count;
}

@genres = ();
$url = "http://watch-movies-az.com/genres/action";
my $response = $browser->get( $url, @ns_headers );
unless ($response->is_success) 
{
	warn "Unable to load '$url'\n";
	next;
}
$string = $response->decoded_content;
while ($string =~ m/<a href="http:\/\/watch-movies-az.com\/genres\/([^"\n]+)[^>]+>/g) 
{
	$genre = $1;
	push (@genres,$genre);
}
$insertGenres = $db->prepare('SELECT F_TMoviesGenres_Add_MovieGenre(?,?,?,?,?);');
foreach $id (@genres)
{
	$baseURL = "http://watch-movies-az.com/genres";
	
	$countLetter->execute();
	$countLetter->bind_columns(\$initialCount);
	$countLetter->fetch();
	$url = "$baseURL/$id";
	my $response = $browser->get( $url, @ns_headers );
	unless ($response->is_success) 
	{
		warn "Unable to load page for '$id'\n";
		next;
	}
	$string = $response->decoded_content;
	while ($string =~ m/<a href="http:\/\/watch-movies-az.com\/movie\/([^"\n]+)[^>]+>\s*\n*\s*(.*)\s+\((\d*)\)\s*<\/a>/g) 
	{
		$code = $1;
		$name = trim($2);
		$year = $3;
		if( substr($name, 0, 1) =~ /[a-zA-Z]/ )
		{
			$char = uc(substr($name, 0, 1));
		}
		else
		{
			$char = '#';
		}
		$insertGenres->execute($name,$year,$char,$code,$id);
	}
	$countLetter->execute();
	$countLetter->bind_columns(\$finalCount);
	$countLetter->fetch();
	$count = $finalCount - $initialCount;
	if( $count > 0 )
	{
		print "$id: $count rows added\n";
	}
	$totalcount = $totalcount + $count;
}
####################################################################################################
####################################################################################################
####################################################################################################
$countLetter->execute();
$countLetter->bind_columns(\$totalRows);
$countLetter->fetch();
print "> TOTAL: $totalcount rows added (Total rows: $totalRows)\n";
$insert->finish();
$countLetter->finish();
$insertGenres->finish();
$db->disconnect();
print "~~~ MOVIELIST.PL END TIME: ".now()." ~~~\n";
